import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_admin/models/odoo/stock_quant.dart';
import 'package:syncfusion_flutter_core/theme.dart';
import 'package:syncfusion_flutter_datagrid/datagrid.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

final textStyle = TextStyle(
    color: Color.fromRGBO(11, 123, 118, 1.0),
    decoration: TextDecoration.none,
    fontSize: 18.sp);

class DataGtid extends StatefulWidget {
  List dataList;

  String? title;

  DataGtid(
      {Key? key,
      required this.dataList,
      required this.title,})
      : super(key: key);

  @override
  _DataGtidState createState() => _DataGtidState();
}

class _DataGtidState extends State<DataGtid> {
  late DataSource _dataSource;
  late Timer timer;

  @override
  Widget build(BuildContext context) {
    return _buildDataGrid();
  }

  @override
  void initState() {
    super.initState();
    _dataSource = DataSource(widget.dataList);
  }

  @override
  void didUpdateWidget(DataGtid oldWifget){
    _dataSource = DataSource(widget.dataList);
  }

  @override
  void dispose() {
    super.dispose();
  }


  _buildDataGrid() {
    return Column(
      children: [
        // Container(
        //   // width: width,
        //   color: Color.fromRGBO(48, 48, 85, 1.0),
        //   child: Padding(
        //     padding: EdgeInsets.all(10),
        //     child: Row(
        //       children: [
        //         Text(
        //           widget.title!,
        //           style: TextStyle(
        //               color: Color.fromRGBO(255, 255, 255, 1),
        //               decoration: TextDecoration.none,
        //               fontSize: 25.sp),
        //         ),
        //       ],
        //     ),
        //   ),
        // ),
        Expanded(
          // width: width,
          // height: 200.h,
          child: SfDataGridTheme(
              data: SfDataGridThemeData(
                gridLineColor: Color.fromRGBO(46, 47, 83, 1.0),
                headerColor: Colors.transparent,
                headerHoverColor: Colors.transparent,
              ),
              child: SfDataGrid(
                columnWidthMode: ColumnWidthMode.fill,
                source: _dataSource,
                rowHeight: 55.h,
                headerRowHeight: 55.h,
                gridLinesVisibility: GridLinesVisibility.both,
                headerGridLinesVisibility: GridLinesVisibility.both,
                columns: [
                  GridColumn(
                      columnName: 'no',
                      maximumWidth: 100.r,
                      label: Container(
                          padding: EdgeInsets.symmetric(horizontal: 16.0),
                          alignment: Alignment.center,
                          child: Text(
                            '序号',
                            style: textStyle,
                            overflow: TextOverflow.ellipsis,
                          ))),
                  GridColumn(
                      columnName: 'productionorderno',
                      // minimumWidth: 270.r,
                      label: Container(
                          padding: EdgeInsets.symmetric(horizontal: 16.0),
                          alignment: Alignment.center,
                          child: Text(
                            '单号',
                            style: textStyle,
                            overflow: TextOverflow.ellipsis,
                          ))),
                  GridColumn(
                      columnName: 'productName',
                      // minimumWidth: 270.r,
                      label: Container(
                          padding: EdgeInsets.symmetric(horizontal: 16.0),
                          alignment: Alignment.center,
                          child: Text(
                            '产品名称',
                            style: textStyle,
                            overflow: TextOverflow.ellipsis,
                          ))),
                  GridColumn(
                      columnName: 'processName',
                      // minimumWidth: 270.r,
                      label: Container(
                          padding: EdgeInsets.symmetric(horizontal: 16.0),
                          alignment: Alignment.center,
                          child: Text(
                            '工序',
                            style: textStyle,
                            overflow: TextOverflow.ellipsis,
                          ))),
                  GridColumn(
                      columnName: 'qualifiedquantity',
                      label: Container(
                          padding: EdgeInsets.symmetric(horizontal: 16.0),
                          alignment: Alignment.center,
                          child: Text(
                            '数量',
                            style: textStyle,
                            overflow: TextOverflow.ellipsis,
                          ))),
                  GridColumn(
                      columnName: 'teamName',
                      // minimumWidth: 270.r,
                      label: Container(
                          padding: EdgeInsets.symmetric(horizontal: 16.0),
                          alignment: Alignment.center,
                          child: Text(
                            '班组',
                            style: textStyle,
                            overflow: TextOverflow.ellipsis,
                          ))),
                  GridColumn(
                      columnName: 'reportworkerName',
                      label: Container(
                          padding: EdgeInsets.symmetric(horizontal: 16.0),
                          alignment: Alignment.center,
                          child: Text(
                            '报工人',
                            style: textStyle,
                            overflow: TextOverflow.ellipsis,
                          ))),
                  GridColumn(
                      columnName: 'reportworkerDate',
                      // minimumWidth: 270.r,
                      label: Container(
                          padding: EdgeInsets.symmetric(horizontal: 16.0),
                          alignment: Alignment.center,
                          child: Text(
                            '报工时间',
                            style: textStyle,
                            overflow: TextOverflow.ellipsis,
                          ))),


                ],
              )),
        ),
      ],
    );
  }
}

class DataSource extends DataGridSource {
  DataSource(List dataList) {
    var index = 1;
    dataGridRows = dataList
        .map<DataGridRow>((data) => DataGridRow(cells: [
              DataGridCell<int>(columnName: 'no', value: index++),
              DataGridCell<String>(columnName: 'productionorderno', value: data['productionorderno']), //物料名称
              DataGridCell<String>(columnName: 'productName', value: data['productName']), //物料名称
              DataGridCell<String>(columnName: 'processName', value: data['processName']), //物料名称
              DataGridCell<String>(columnName: 'qualifiedquantity', value: data['qualifiedquantity']), //单价
              DataGridCell<String>(columnName: 'teamName', value: data['teamName']), //单价
              DataGridCell<String>(columnName: 'reportworkerName', value: data['reportworkerName']), //数量
              DataGridCell<String>(columnName: 'reportworkerDate', value: data['reportworkerDate']), //物料名称

    ]))
        .toList();
  }

  List<DataGridRow> dataGridRows = [];

  @override
  List<DataGridRow> get rows => dataGridRows;

  @override
  DataGridRowAdapter? buildRow(DataGridRow row) {
    return DataGridRowAdapter(
        color: Colors.transparent,
        cells: row.getCells().map<Widget>((dataGridCell) {
          return Container(
                  alignment: Alignment.center,
                  padding: EdgeInsets.symmetric(horizontal: 16.0),
                  child: Text(
                    dataGridCell.value.toString(),
                    style: TextStyle(
                        fontSize: 18.sp,
                        color: Colors.white,
                        decoration: TextDecoration.none),
                    overflow: TextOverflow.ellipsis,
                  ));
        }).toList());
  }
}
